<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教师名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入教师名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="queryParams.price" placeholder="请输入价格" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.sys_teacher_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:teacher:add']" v-if="roleCode == 'admin' || roleCode == 'teacher'">申请为家教教师</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:teacher:remove']" v-if="roleCode == 'admin' || roleCode == 'teacher'">删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teacherList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="教师名称" align="center" prop="name" />
      <el-table-column label="学科" align="center" prop="subjectName" />
      <el-table-column label="教师评分" align="center" prop="teacherScore" />
      <el-table-column label="价格" align="center" prop="price" />

      <el-table-column label="申请材料" align="center" prop="applicationFile" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.applicationFile" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_teacher_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:teacher:edit']" v-if="roleCode == 'admin' || roleCode == 'teacher'">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleHireTeacher(scope.row)"
             v-if="roleCode == 'parent'">聘请教师</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleApplicationSuccess(scope.row)"
            v-hasPermi="['system:teacher:edit']"
            v-if="roleCode == 'admin' && scope.row.status == 'ADMIN_APPLICATION'">申请通过</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleApplicationFail(scope.row)"
            v-hasPermi="['system:teacher:edit']"
            v-if="roleCode == 'admin' && scope.row.status == 'ADMIN_APPLICATION'">申请驳回</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:teacher:remove']" v-if="roleCode == 'admin' || roleCode == 'teacher'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改家教教师对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学科" prop="subjectId">
          <el-select v-model="form.subjectId" placeholder="学科" clearable>
            <el-option v-for="dict in subjectList" :key="dict.id" :label="dict.name" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="申请材料" prop="applicationFile">
          <image-upload v-model="form.applicationFile" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="聘请教师" :visible.sync="openHire" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="家属" prop="studentId">
          <el-select v-model="form.studentId" placeholder="家属" clearable>
            <el-option v-for="dict in familyList" :key="dict.childId" :label="dict.name" :value="dict.childId" />
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitHireForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeacher, getTeacher, delTeacher, addTeacher, updateTeacher, changeTeacherApplicationStatus, hireTeacher } from "@/api/system/teacher";
import { getAllSubjectList } from "@/api/system/subject";
import { listFamily } from "@/api/system/family";

export default {
  name: "Teacher",
  dicts: ['sys_teacher_status'],
  data() {
    return {
      openHire: false,
      familyList: [],
      roleCode: null,
      subjectList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 家教教师表格数据
      teacherList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      teacherId: null ,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        price: null,
        subjectId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.roleCode = localStorage.getItem("roles")
    console.log(this.roleCode)
    this.getList();
    getAllSubjectList().then(res => {
      this.subjectList = res.rows
    })
    listFamily().then(res => {
      this.familyList = res.rows
    })
  },
  methods: {
    handleHireTeacher(info) {
      this.openHire = true
      this.teacherId = info.userId
      this.reset();
    },
    handleApplicationSuccess(info) {
      info.status = 'APPLICATION_SUCCESS'
      changeTeacherApplicationStatus(info).then(res => {
        if (res.code == 200) {
          this.$modal.msgSuccess("操作成功")
        } else {
          this.$modal.msgError("操作失败")
        }
        this.getList();
      })
    },
    handleApplicationFail(info) {
      info.status = 'APPLICATION_REJECT'

      changeTeacherApplicationStatus(info).then(res => {
        if (res.code == 200) {
          this.$modal.msgSuccess("操作成功")
        } else {
          this.$modal.msgError("操作失败")
        }
        this.getList();
      })
    },
    /** 查询家教教师列表 */
    getList() {
      this.loading = true;
      listTeacher(this.queryParams).then(response => {
        this.teacherList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openHire = false
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        price: null,
        subjectId: null,
        applicationFile: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加家教教师";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTeacher(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改家教教师";
      });
    },
    submitHireForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.teacherId = this.teacherId
          hireTeacher(this.form).then(res => {
            if (res.code == 200) {
              this.$modal.msgSuccess("聘请成功!");
            } else {
              this.$modal.msgError(res.meg);
            }
            this.open = false;
            this.getList();
          })
        }
      });
      this.openHire = false
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTeacher(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeacher(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除家教教师编号为"' + ids + '"的数据项？').then(function () {
        return delTeacher(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/teacher/export', {
        ...this.queryParams
      }, `teacher_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
